package com.jar.exercise.多线程.synchroniz;

/**
 * <div  style="border:2px solid none;margin:20px;padding:15px;font-size:24px;text-align:center;color:aqua;">
 *
 *  同步代码块
 *
 *  </div>
 *
 * @author RichMan
 * @date 2021-03-13 19:31
 */
public class 代码块上SynchronizedTest {

    public void t1(){

        System.out.println("---- 进入 t1 ----");
        synchronized (Sub2Class.class) {

            try {
                System.out.println("---- 开始 t1 ----");
                Thread.sleep(3000);
                System.out.println("---- 结束 t1 ----");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("---- 出去 t1 ----");

    }


    public void t2(){

        System.out.println("---- 进入 t2 ----");
    synchronized (Sub2Class.class){

        try {
            System.out.println("---- 开始 t2 ----");
            Thread.sleep(3000);
            System.out.println("---- 结束 t2 ----");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

        System.out.println("---- 出去 t2 ----");

    }

    static class SubClass{
        public static void main(String[] args) {
            代码块上SynchronizedTest a = new 代码块上SynchronizedTest();

            new Thread(() -> {
                a.t1();
            }).start();

            new Thread(() -> {
                a.t2();
            }).start();
        }
    }

    static class Sub2Class{

    }

}
